45C - Dancing Lessons - CodeForces Solution


data structures *1900

Please click on ads to support us..

Python Code:

def read_int():
    return int(input())
    
def read_array():
    return list(map(int, input().split()))

n = read_int()
s = input()
levels = read_array()

import heapq

h = []

b, g = 0, 0
for i in range(len(s)):
    if i > 0 and s[i] != s[i - 1]:
        heapq.heappush(h, (abs(levels[i] - levels[i - 1]), i - 1, i))
    if s[i] == 'B':
        b += 1
    elif s[i] == 'G':
        g += 1

pr = [i for i in range(n)]
pl = [i for i in range(n)]
sz = [1 for _ in range(n)]

def find(v, p):
    if v < 0 or v == n:
        return v
    if v == p[v]:
        return v
        
    p[v] = find(p[v], p)
    return p[v]

def union(u, v, p):
    a = find(u)
    b = find(v)
    
    if a == b:
        return
    
    if sz[a] < sz[b]:
        a, b = b, a
    
    p[a] = p[b]
    sz[a] += sz[b]

ans = []
x = min(b, g)
deleted = set()
while x > 0:
    _, l, r = heapq.heappop(h)
    if l in deleted or r in deleted:
        continue

    x -= 1
    ans.append((l, r))

    deleted.add(l)
    deleted.add(r)

    pr[l] = r + 1
    pr[r] = r + 1

    pl[l] = l - 1
    pl[r] = l - 1
    
    r, l = find(l, pr), find(r, pl)

    if l >= 0 and r < len(s) and s[l] != s[r]:
        heapq.heappush(h, (abs(levels[l] - levels[r]), l, r))

print(len(ans))
for l, r in ans:
    print(l+1, r+1)

C++ Code:

#include <stdio.h>



#define abs(a) ((a)<0?-(a):(a))



char str[200005]={'\0'};

long a[200005]={0};

long next[200005]={0};

long last[200005]={0};

long ans[200005][2]={0};

long calc=0;



struct case1

{

 long num;

 long x,y;

}heap[200005]={{0,0,0}};



long tot=0;

long trans[200005][2]={0};



void swap(long i,long j)

{

 struct case1 temp;

 

 trans[heap[i].x][0]=j;

 trans[heap[i].y][1]=j;

 trans[heap[j].x][0]=i;

 trans[heap[j].y][1]=i;

 

 temp=heap[i];

 heap[i]=heap[j];

 heap[j]=temp;

}



void up(long x)

{

 if(x>tot)

  return;

 

 for(;x>1;x>>=1)

   {

    if(heap[x].num>heap[x>>1].num||heap[x].num==heap[x>>1].num&&heap[x].x>heap[x>>1].x)

     return;

    swap(x,x>>1);

   }

}



void down(long x)

{

 for(;x*2<=tot;)

   {

    if((heap[x].num<heap[x*2].num||heap[x].num==heap[x*2].num&&heap[x].x<heap[x*2].x)&&(heap[x].num<heap[x*2+1].num||heap[x].num==heap[x*2+1].num&&heap[x].x<heap[x*2+1].x||x*2+1>tot))

     return;

    if(heap[x*2].num<heap[x*2+1].num||heap[x*2].num==heap[x*2+1].num&&heap[x*2].x<heap[x*2+1].x||x*2+1>tot)

     {

      swap(x,x*2);

      x=x*2;

     }

    else

     {

      swap(x,x*2+1);

      x=x*2+1;

     }

   }

}



void del(long x)

{

 if(x)

  {

   swap(x,tot);

   trans[heap[tot].x][0]=0;

   trans[heap[tot].y][1]=0;

   heap[tot].num=heap[tot].x=heap[tot].y=0;

   tot--;

   up(x);

   down(x);

  }

}



int main()

{

 long n,i,x,y;

 

 scanf("%ld",&n);

 scanf("%s",str+1);

 for(i=1;i<=n;i++)

   scanf("%ld",&a[i]);

 

 for(i=1;i<=n;i++) 

   {

    next[i]=i+1;

    last[i]=i-1;

   }

 

 for(i=1;i<n;i++)

   {

    if(str[i]!=str[i+1])

     {

      heap[++tot].x=i;

      heap[tot].y=i+1;

      heap[tot].num=abs(a[i]-a[i+1]);

      trans[i][0]=tot;

      trans[i+1][1]=tot;

      up(tot);

     }

   }

 

 for(;tot;)

   {

    x=heap[1].x;

    y=heap[1].y;

    ans[++calc][0]=x;

    ans[calc][1]=y;

    del(trans[x][0]);

    del(trans[x][1]);

    del(trans[y][0]);

    del(trans[y][1]);

    if(next[y]<=n)

     last[next[y]]=last[x];

    if(last[x]>=1)

     next[last[x]]=next[y];

    if(next[y]<=n&&last[x]>=1&&str[next[y]]!=str[last[x]])

     {

      heap[++tot].x=last[x];

      heap[tot].y=next[y];

      heap[tot].num=abs(a[next[y]]-a[last[x]]);

      trans[last[x]][0]=tot;

      trans[next[y]][1]=tot;

      up(tot);

     }

   }

 

 printf("%ld\n",calc);

 for(i=1;i<=calc;i++)

   {

    printf("%ld %ld\n",ans[i][0],ans[i][1]);

   }

 

 return 0;

}


Comments

Submit
0 Comments
More Questions

1699B - Almost Ternary Matrix
1545A - AquaMoon and Strange Sort
538B - Quasi Binary
424A - Squats
1703A - YES or YES
494A - Treasure
48B - Land Lot
835A - Key races
1622C - Set or Decrease
1682A - Palindromic Indices
903C - Boxes Packing
887A - Div 64
755B - PolandBall and Game
808B - Average Sleep Time
1515E - Phoenix and Computers
1552B - Running for Gold
994A - Fingerprints
1221C - Perfect Team
1709C - Recover an RBS
378A - Playing with Dice
248B - Chilly Willy
1709B - Also Try Minecraft
1418A - Buying Torches
131C - The World is a Theatre
1696A - NIT orz
1178D - Prime Graph
1711D - Rain
534A - Exam
1472A - Cards for Friends
315A - Sereja and Bottles